package com.wolfsea.launcherdemo
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.core.view.get

/**
 *@desc HotSeaLayout
 *@author liuliheng
 *@time 2022/12/24  22:37
 **/
class HotSeaLayout @JvmOverloads constructor(context: Context, attributeSet: AttributeSet? = null) : LinearLayoutCompat(context,attributeSet) {

    override fun onFinishInflate() {
        super.onFinishInflate()
        val contentDataList = mutableListOf<ContentData>()

        val dataArray = arrayOf(
            "知名电子",
            "掌机",
            "掌上游戏",
            "设计师",
        )

        for (item in dataArray) {
            contentDataList.add(ContentData(R.mipmap.ic_launcher, item))
        }

        addContent(contentDataList)
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        var calHeight = 0
        for (i in 0.until(childCount)) {
            val child = get(i)
            measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0)
            val childMeasureWidth = child.measuredWidth - child.paddingStart - child.paddingEnd
            val layoutParams = child.layoutParams as MarginLayoutParams
            val itemSpace = ((measuredWidth - BaseConstant.ITEMS_PER_ROW * childMeasureWidth).toFloat() / (BaseConstant.ITEMS_PER_ROW + 1)).toInt()
            layoutParams.leftMargin = itemSpace
            child.layoutParams = layoutParams
            calHeight = child.measuredHeight - child.paddingTop - child.paddingBottom + layoutParams.topMargin
        }

        setMeasuredDimension(
            MeasureSpec.getSize(widthMeasureSpec) - paddingStart - paddingEnd,
            MeasureSpec.getSize(
                MeasureSpec.makeMeasureSpec(
                    calHeight,
                    MeasureSpec.EXACTLY
                )
            ) - paddingTop - paddingBottom
        )
    }

    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        super.onLayout(changed, l, t, r, b)

        var totalChildWidth = 0

        for (i in 0.until(childCount)) {

            val child = get(i)
            val childMeasuredWidth = child.measuredWidth - child.paddingStart - child.paddingEnd
            val childMeasuredHeight = child.measuredHeight

            val layoutParams = child.layoutParams as MarginLayoutParams
            val topMargin = layoutParams.topMargin
            val leftMargin = layoutParams.leftMargin

            val cl = leftMargin + totalChildWidth
            val cr = cl + childMeasuredWidth
            val ct = topMargin - child.paddingTop - child.paddingBottom
            val cb = ct + childMeasuredHeight

            child.layout(
                cl,
                ct,
                cr,
                cb
            )

            totalChildWidth += leftMargin + childMeasuredWidth
        }
    }

    private fun addContent(contentDataSet: MutableList<ContentData>) {
        for (i in contentDataSet.indices) {
            val contentData = contentDataSet[i]
            val layout = LayoutInflater.from(context).inflate(R.layout.layout_content, this, false)
            val llContent = layout.findViewById<LinearLayoutCompat>(R.id.ll_content)
            llContent.tag = i
            llContent.setOnClickListener {
                context.showToast(contentData.contentDesc)
            }
            llContent.setOnLongClickListener {
                //val item = ClipData.Item("")
                //val clipData = ClipData("", arrayOf(ClipDescription.MIMETYPE_TEXT_PLAIN), item)
                //val shadow = DragShadowBuilder(it)
                //shadow.view.alpha = 0F
                //it.startDragAndDrop(clipData, shadow, null, 0)
                return@setOnLongClickListener true
            }
            val contentIv = layout.findViewById<AppCompatImageView>(R.id.iv_content)
            contentIv.setBackgroundResource(contentData.drawRes)
            val contentTv = layout.findViewById<AppCompatTextView>(R.id.tv_content)
            contentTv.text = contentData.contentDesc
            contentTv.visibility = View.GONE
            addView(
                layout,
                MarginLayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT
                ).apply {
                    topMargin = context.resources.getDimension(R.dimen.dp_20).toInt()
                }
            )
        }
    }

}